package com.example.webframework;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RestController;

import java.util.Base64;
import java.util.HashMap;
import java.util.Map;

@SpringBootApplication
@RestController
public class WebFrameworkApplication {

    public static void main(String[] args) {
        SpringApplication.run(WebFrameworkApplication.class, args);
    }

    @GetMapping("/")
    public ResponseEntity<String> welcome() {
        String welcome = "<!DOCTYPE html>\n" +
                "<html lang=\"en\">\n" +
                "<head>\n" +
                "    <meta charset=\"UTF-8\">\n" +
                "    <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n" +
                "    <title>Welcome</title>\n" +
                "    <style>\n" +
                "        body {\n" +
                "            display: flex;\n" +
                "            justify-content: center;\n" +
                "            align-items: center;\n" +
                "            height: 100vh;\n" +
                "            margin: 0;\n" +
                "        }\n" +
                "        h1 {\n" +
                "            font-size: 36px;\n" +
                "        }\n" +
                "        .content {\n" +
                "            position: relative;\n" +
                "            padding-left: 30px;\n" +
                "            color: #D4001E;\n" +
                "        }\n" +
                "        .content::before {\n" +
                "            content: '';\n" +
                "            position: absolute;\n" +
                "            top: 50%;\n" +
                "            left: 0;\n" +
                "            transform: translateY(-50%);\n" +
                "            width: 10px;\n" +
                "            height: 168px;\n" +
                "            background-color: #D4001E;\n" +
                "        }\n" +
                "    </style>\n" +
                "</head>\n" +
                "<body>\n" +
                "    <div class=\"content\">\n" +
                "        <img src=\"\" alt=\"天翼云\">\n" +
                "        <h1>Welcome to Spring Boot!</h1>\n" +
                "        <p>本应用由天翼云函数计算Serverless应用创建</p>\n" +
                "    </div>\n" +
                "</body>\n" +
                "</html>";

        String log = "Hello log in Spring Boot!";
        byte[] encoded = Base64.getEncoder().encode(log.getBytes());
        return ResponseEntity.ok()
                // 通过HTTP响应头x-fc-log-result设置base64日志内容
                .header("x-fc-log-result", new String(encoded))
                // 设置响应体
                .body(welcome);
    }

    @GetMapping("/fcheaders")
    public ResponseEntity<Map<String, String>> listHeaders(
            @RequestHeader Map<String, String> headers) {
        Map<String, String> fcHeaders = new HashMap<>();
        headers.forEach((key, value) -> {
            if (key.startsWith("x-fc")) {
                fcHeaders.put(key, value);
            }

        });

        return new ResponseEntity<>(fcHeaders, HttpStatus.OK);
    }
}
